//
//  EditContactController.m
//  OCQ_API_Test
//
//  Created by 故乡的云  on 2017/5/4.
//  Copyright © 2017年 故乡的云 . All rights reserved.
//
// 创建和编辑联系人

#import "EditContactController.h"
#import "gxdy_ContactPropertyCell.h"
#import "gxdy_ContactPropertyModel.h"
#import "Gxdy_ContactModel.h"

@interface EditContactController ()
@property(nonatomic, strong) Gxdy_ContactModel *contactM;
@property(nonatomic, strong) NSMutableArray *contactProterties;
@end

@implementation EditContactController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.view.backgroundColor = gxdy_GlobalBg;
    
    self.tableView.separatorColor = [UIColor clearColor];
    //    self.tableView.backgroundColor = [UIColor orangeColor];
    self.tableView.allowsSelection = NO;
    __weak EditContactController *weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save:)];
    
    
}

- (void)save:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];

}

/// 初始化数据
- (void)loadNewData {
    __weak EditContactController *weakSelf = self;
    NSString *urlStr = @"http://10.10.2.184:1080/mobileApp/queryCustomizableLayout";
    
    NSDictionary *paras = @{
                            @"entityName" : @"Contact",
                            @"entityId" : @"003-b892129b-d052-45e4-9077-0efee3707906",
                                    
                            @"relativeId" : @"",
//                            @"sessionId" : [UserModel sharedUserModel].sessionId
                            };
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:urlStr parameters:paras progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 结束刷新
        [weakSelf.tableView.mj_header endRefreshing];
        
        if ([[responseObject allKeys] containsObject:@"error"]) {
            gxdy_Log(@"请求数据失败：%@",responseObject);
            return ;
        }
        
        weakSelf.contactM = [Gxdy_ContactModel mj_objectWithKeyValues:responseObject[@"data"]];
        NSArray *sections = responseObject[@"layout"][@"sections"];
        NSArray *rows = sections[0][@"rows"];
        //        NSArray *cells = rows[0][@"cells"];
        [rows enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            gxdy_ContactPropertyModel *model = [gxdy_ContactPropertyModel mj_objectWithKeyValues:obj[@"cells"][0]];
            [weakSelf.contactProterties addObject:model];
        }];
        [weakSelf.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 结束刷新
        [weakSelf.tableView.mj_header endRefreshing];
        gxdy_Log(@"error!");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.contactProterties.count == 0) {
        return 0;
    }
    return self.contactProterties.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    gxdy_ContactPropertyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [gxdy_ContactPropertyCell cell];
    }
    //    cell.editing = self.allowEdit;
    cell.contactModel = self.contactProterties[indexPath.row];
    cell.model = self.contactM;
    return cell;
}

- (NSMutableArray *)contactProterties {
    if (_contactProterties == nil) {
        _contactProterties = [NSMutableArray array];
    }
    return _contactProterties;
}



#pragma mark - Navigation



@end
